package com.seocoo.worker.friend;

import android.Manifest;
import android.support.v7.widget.ViewUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.seocoo.worker.R;
import com.seocoo.worker.base.PermissionUtil;
import com.seocoo.worker.base.entity.EFile;
import com.seocoo.worker.base.http.BasePageRsp;
import com.seocoo.worker.base.refresh.SwipeRefreshMoreLayout;
import com.seocoo.worker.base.widget.EditTextCloseView;
import com.seocoo.worker.friend.adapter.FriendAdapter;
import com.seocoo.worker.friend.entity.Friend;
import com.seocoo.worker.friend.module.FriendModel;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.MVVMFragment;
import com.sfh.lib.ui.TextChangedListener;
import com.sfh.lib.ui.UtilRxView;

import java.util.List;
import java.util.concurrent.Future;

/**
 * 功能描述:
 *
 * @author
 * @date 2020/2/16
 */
public class FriendFragment extends MVVMFragment {

    private EditTextCloseView tvSearch;
    private SwipeRefreshMoreLayout swipe_refresh_more_layout;
    private FriendAdapter mAdapter;

    @Override
    public int getLayout() {
        return R.layout.friend_fragment;
    }

    @Override
    public void initData(View var1) {
        tvSearch = var1.findViewById(R.id.tv_search);
        Future future = UtilRxView.textChanges(tvSearch, 1000, s -> {
            // 打开搜索
            swipe_refresh_more_layout.onAutoRefresh();
        });
        putFuture(future);
        swipe_refresh_more_layout = (SwipeRefreshMoreLayout) var1.findViewById(R.id.recycler_view);

        mAdapter = new FriendAdapter();
        swipe_refresh_more_layout.setAdapter(mAdapter, true);
        swipe_refresh_more_layout.setOnLoadingListener(nowPage -> getViewModel(FriendModel.class).queryFirendList(nowPage, tvSearch.getText().toString()));
        swipe_refresh_more_layout.setOnItemClickListener((adapter, view, position) -> FriendDetailActivity.start(getContext(),mAdapter.getItem(position)));
        swipe_refresh_more_layout.onAutoRefresh();
    }

    @Override
    public void onResume() {
        super.onResume();
        PermissionUtil.checkPermissions(FriendFragment.this, "请开启定位权限", 100, new PermissionUtil.PermissionCallBack() {
            @Override
            public void onHasPermission() {

            }

            @Override
            public void onFailedPermission() {

            }
        },Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION);
    }

    @LiveDataMatch
    public void onFriendList(BasePageRsp<Friend> page) {
        swipe_refresh_more_layout.onRefreshCompale(page);
    }

}
